local firemode_rule = fk.CreateSkill {
    name = "#cf_firemode_rule",
}

local function reward_Newskill(room, players)
    local room = room
    local req = Request:new(players, "CustomDialog")
    req.focus_text = "无限火力"
    for _, player in ipairs(players) do
        local existingGenerals = {}
        table.forEach(room.players, function(p)
            table.insertTable(existingGenerals, { p.general, p.deputyGeneral })
        end)
        local generals = table.map(Fk:getGeneralsRandomly(10, Fk:getAllGenerals(), existingGenerals,
            (function(p) return false end)), function(g) return g.name end)
        generals = table.random(generals, math.min(4, #generals))
        local skills = {}
        local choices = {}
        for _, general_name in ipairs(generals) do
            local general = Fk.generals[general_name]
            local g_skills = {}
            
            for _, skill in ipairs(general.skills) do
                table.insertIfNeed(g_skills, skill.name)
            end
            for _, s_name in ipairs(general.other_skills) do
                local skill = Fk.skills[s_name]
                if skill then
                    table.insertIfNeed(g_skills, skill.name)
                end
            end
            table.insertIfNeed(skills, g_skills)
            if #choices == 0 and #g_skills > 0 then
                choices = { g_skills[1] }
            end
        end


        if #choices > 0 then
            req:setData(player, {
                path = "packages/utility/qml/ChooseGeneralSkillsBox.qml",
                data = {
                    generals, skills, 1, 1, "cf_firemode_reward", false
                },
            })
            local s = table.random(skills, 1)[1]
            req:setDefaultReply(player, s[1])
        end
    end


    for _, p in ipairs(players) do
        local choice = req:getResult(p)
        room:handleAddLoseSkills(p, choice, nil)
        p:drawCards(2)
    end
end

firemode_rule:addEffect(fk.GameStart, {
    can_refresh = function(self, event, target, player, data)
        return target == player
    end,
    on_refresh = function(self, event, target, player, data)
        local room = player.room
        local players = room.alive_players
        room:doBroadcastNotify("ShowToast", "无限火力，无尽痛楚")
        local skill_list = { "cf_firemode_liuanhuaming", "cf_firemode_mianmianjudao", "cf_firemode_xushidaifa",
            "cf_firemode_duoduoyishan" }
        local skill = skill_list[math.random(1, 4)]
        for _, p in ipairs(players) do
            room:handleAddLoseSkills(p, "m_bahu", nil, false, true)
            room:handleAddLoseSkills(p, skill, nil, false, true)
        end
    end
})

firemode_rule:addEffect(fk.Damage, {
    can_refresh = function(self, event, target, player, data)
        return target == player
    end,
    on_refresh = function(self, event, target, player, data)
        local room = player.room
        local times = room:getBanner("cf_firemode_times") or 0
        local assign = room:getBanner("cf_firemode_as")
        local count = room:getBanner("cf_firemode_count") or 0

        if times <= 3 then
            count = count + data.damage
            room:setBanner("@cf_firemode_assignmentprocess", tostring(count) .. " / " .. assign)

            if count >= assign then
                room:doBroadcastNotify("ShowToast", "请选择一个技能获得！")

                local players = room.alive_players
                reward_Newskill(room, players)

                local new_times = times + 1
                room:setBanner("cf_firemode_times", new_times)

                local remaining = count - assign
                room:setBanner("cf_firemode_count", remaining)
                room:setBanner("@cf_firemode_assignmentprocess", tostring(remaining) .. " / " .. assign)

                if new_times >= 4 then
                    room:setBanner("@cf_firemode_assignmentprocess", "已获得四次技能，无法继续获得")
                end
            else
                room:setBanner("cf_firemode_count", count)
            end
        end
    end
})

return firemode_rule